﻿using DesktopShortcutMonitor.Model;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DesktopShortcutMonitor.Monitor
{
    public class App
    {
        #region 属性单例模式

        private static readonly Lazy<App> Instance = new Lazy<App>(() => new App());

        private App()
        {

        }
        public static App Ins { get; } = Instance.Value;
        #endregion

        #region 配置
        public Config Config { get; set; }
        public readonly string FilePathParams = Path.Combine(Environment.CurrentDirectory, "Config", "Config.xml");

        public void Init()
        {
            if (!Directory.Exists(Path.GetDirectoryName(FilePathParams)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(FilePathParams));
            }

            LoadDic();
        }

        public void SaveDic()
        {
            try
            {
                MyJosn.SaveExt<Config>(FilePathParams, Config);
            }
            catch (Exception e)
            {
                UIMessageBox.ShowError($"保存配置{FilePathParams}时发生了异常\r{e.Message}\r{e.StackTrace}");
            }
            finally
            {

            }
        }


        public void LoadDic()
        {
            try
            {
                if (!File.Exists(FilePathParams))
                {
                    Config = new Config();
                    UIMessageBox.ShowInfo($"初次使用，没有找到配置文件，已经为您设置了默认规则，请检查是否符合要求");
                    SaveDic();
                    return;
                }
                Config = MyJosn.Load<Config>(FilePathParams);
            }
            catch (Exception e)
            {
                UIMessageBox.ShowError($"加载配置{FilePathParams}时发生了异常\r{e.Message}\r{e.StackTrace}");
            }
            finally
            {

            }
        }
        #endregion
    }
}
